package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.AskAnswerGroupDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 问卷调查组
 * */
@RestController
@RequestMapping("AskAnswerGroup")
public class AskAnswerGroupController extends BaseController{



    @Autowired
    @Qualifier("AskAnswerGroupServiceImplOne")
    private AskAnswerGroupService askAnswerGroupService;

    @Autowired
    @Qualifier("AskAnswerItemServiceImplOne")
    private AskAnswerItemService askAnswerItemService;

    @Autowired
    @Qualifier("CommandCenterStaffRelServiceImplOne")
    private CommandCenterStaffRelService commandCenterStaffRelService;

    @Autowired
    @Qualifier("AskAnswerNameServiceImplOne")
    private AskAnswerNameService askAnswerNameService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("groupName");
            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");
            int sort = jsonObject.getInteger("sort");
            JSONArray arr = jsonObject.getJSONArray("arr");

            String staffCode = jsonObject.getString("staffCode");

            String type = jsonObject.getString("type");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerGroup/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            Staff staff = staffService.getByStaffCode(staffCode);

            AskAnswerGroup askAnswerGroup =new AskAnswerGroup();
            askAnswerGroup.setAskAnswerGroupCode(UUID.randomUUID().toString());
            askAnswerGroup.setAskAnswerNameCode(askAnswerNameCode);
            askAnswerGroup.setName(name);
            askAnswerGroup.setStatus(1);
            askAnswerGroup.setSort(sort);
            askAnswerGroup.setGovernmentCode(staff.getGovernmentCode());
            askAnswerGroup.setCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());
            askAnswerGroup.setType(type);
            askAnswerGroupService.insert(askAnswerGroup);


            for(int i=0;i<arr.size();i++){
                AskAnswerItem askAnswerItem = new AskAnswerItem();
                askAnswerItem.setAskAnswerItemCode(UUID.randomUUID().toString());
                askAnswerItem.setAskAnswerGroupCode(askAnswerGroup.getAskAnswerGroupCode());
                askAnswerItem.setName(arr.getJSONObject(i).getString("itemName"));
                askAnswerItem.setFraction(arr.getJSONObject(i).getInteger("fraction"));
                askAnswerItemService.insert(askAnswerItem);
            }


            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerGroup/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovAdd", method = RequestMethod.POST)
    public MyResult GovAdd(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("groupName");
            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");
            int sort = jsonObject.getInteger("sort");
            JSONArray arr = jsonObject.getJSONArray("arr");

            String type = jsonObject.getString("type");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerGroup/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            AskAnswerGroup askAnswerGroup =new AskAnswerGroup();
            askAnswerGroup.setAskAnswerGroupCode(UUID.randomUUID().toString());
            askAnswerGroup.setAskAnswerNameCode(askAnswerNameCode);
            askAnswerGroup.setName(name);
            askAnswerGroup.setStatus(1);
            askAnswerGroup.setSort(sort);
            askAnswerGroup.setGovernmentCode(staff.getGovernmentCode());
            //askAnswerGroup.setCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());
            askAnswerGroup.setType(type);
            askAnswerGroupService.insert(askAnswerGroup);


            for(int i=0;i<arr.size();i++){
                AskAnswerItem askAnswerItem = new AskAnswerItem();
                askAnswerItem.setAskAnswerItemCode(UUID.randomUUID().toString());
                askAnswerItem.setAskAnswerGroupCode(askAnswerGroup.getAskAnswerGroupCode());
                askAnswerItem.setName(arr.getJSONObject(i).getString("itemName"));
                askAnswerItem.setFraction(arr.getJSONObject(i).getInteger("itemFraction"));
                askAnswerItem.setSort(arr.getJSONObject(i).getInteger("itemSort"));
                askAnswerItemService.insert(askAnswerItem);
            }


            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerGroup/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("groupName");
            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");
            int status = jsonObject.getInteger("status");
            int sort = jsonObject.getInteger("sort");
            JSONArray arr = jsonObject.getJSONArray("arr");

            String staffCode = jsonObject.getString("staffCode");

            String askAnswerGroupCode = jsonObject.getString("askAnswerGroupCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerGroup/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            AskAnswerGroup askAnswerGroup = askAnswerGroupService.getByAskAnswerGroupCode(askAnswerGroupCode);
            askAnswerGroup.setName(name);
            askAnswerGroup.setStatus(status);
            askAnswerGroup.setSort(sort);
            askAnswerGroup.setAskAnswerNameCode(askAnswerNameCode);
            askAnswerGroupService.update(askAnswerGroup);

            List<AskAnswerItem> list = askAnswerItemService.ListByAskAnswerGroupCode(askAnswerGroupCode);
            for(AskAnswerItem askAnswerItem:list){
                askAnswerItemService.delete(askAnswerItem.getAskAnswerItemCode());
            }

            for(int i=0;i<arr.size();i++){
                AskAnswerItem askAnswerItem = new AskAnswerItem();
                askAnswerItem.setAskAnswerItemCode(UUID.randomUUID().toString());
                askAnswerItem.setAskAnswerGroupCode(askAnswerGroup.getAskAnswerGroupCode());
                askAnswerItem.setName(arr.getJSONObject(i).getString("itemName"));
                askAnswerItem.setFraction(arr.getJSONObject(i).getInteger("itemFraction"));
                askAnswerItem.setSort(arr.getJSONObject(i).getInteger("itemSort"));
                askAnswerItemService.insert(askAnswerItem);
            }


            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerGroup/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //根据问卷调查名称查列表
    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult List(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerGroup/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<AskAnswerGroup> list = askAnswerGroupService.listByAskAnswerNameCode(askAnswerNameCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",list);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerGroup/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //根据问卷调查名称查列表包括项
    @RequestMapping(value = "ListAndItem", method = RequestMethod.POST)
    public MyResult ListAndItem(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerGroup/ListAndItem");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<AskAnswerGroup> list = askAnswerGroupService.listByAskAnswerNameCode(askAnswerNameCode);

            List<AskAnswerGroupDTO> listDTO = new ArrayList<>();
            for(AskAnswerGroup askAnswerGroup :list){
                AskAnswerGroupDTO askAnswerGroupDTO = mapper.map(askAnswerGroup,AskAnswerGroupDTO.class);

                AskAnswerName askAnswerName = askAnswerNameService.getByAskAnswerNameCode(askAnswerGroupDTO.getAskAnswerNameCode());
                askAnswerGroupDTO.setAskAnswerName(askAnswerName.getName());

                List<AskAnswerItem> listItem = askAnswerItemService.ListByAskAnswerGroupCode(askAnswerGroup.getAskAnswerGroupCode());
                askAnswerGroupDTO.setListAskAnswerItem(listItem);

                listDTO.add(askAnswerGroupDTO);
            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("listDTO",listDTO);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerGroup/ListAndItem",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //指挥中心
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerGroup/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            PageInfo<AskAnswerGroup> pageInfo = askAnswerGroupService.pageByCommandCenterCodeAskAnswerNameCode(pageIndex,pageSize,
                    commandCenterStaffRel.getCommandCenterCode(),askAnswerNameCode);

            List<AskAnswerGroupDTO> listDTO = new ArrayList<>();
            for(AskAnswerGroup askAnswerGroup:pageInfo.getList()){
                AskAnswerGroupDTO askAnswerGroupDTO = mapper.map(askAnswerGroup,AskAnswerGroupDTO.class);

                AskAnswerName askAnswerName = askAnswerNameService.getByAskAnswerNameCode(askAnswerGroupDTO.getAskAnswerNameCode());
                askAnswerGroupDTO.setAskAnswerName(askAnswerName.getName());

                List<AskAnswerItem> listAskAnswerItem = askAnswerItemService.ListByAskAnswerGroupCode(askAnswerGroupDTO.getAskAnswerGroupCode());
                askAnswerGroupDTO.setListAskAnswerItem(listAskAnswerItem);
                listDTO.add(askAnswerGroupDTO);
            }

            PageInfo<AskAnswerGroupDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerGroup/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovPage",method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerGroup/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            //CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            PageInfo<AskAnswerGroup> pageInfo = askAnswerGroupService.pageByGovCodeTypeAskAnswerNameCode(pageIndex,pageSize,
                    staff.getGovernmentCode(),"train",askAnswerNameCode);

            List<AskAnswerGroupDTO> listDTO = new ArrayList<>();
            for(AskAnswerGroup askAnswerGroup:pageInfo.getList()){
                AskAnswerGroupDTO askAnswerGroupDTO = mapper.map(askAnswerGroup,AskAnswerGroupDTO.class);

                AskAnswerName askAnswerName = askAnswerNameService.getByAskAnswerNameCode(askAnswerGroupDTO.getAskAnswerNameCode());
                askAnswerGroupDTO.setAskAnswerName(askAnswerName.getName());

                List<AskAnswerItem> listAskAnswerItem = askAnswerItemService.ListByAskAnswerGroupCode(askAnswerGroupDTO.getAskAnswerGroupCode());
                askAnswerGroupDTO.setListAskAnswerItem(listAskAnswerItem);
                listDTO.add(askAnswerGroupDTO);
            }

            PageInfo<AskAnswerGroupDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerGroup/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
